﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using PARK.Attend.Entitys.Dto.AttendRule;
using PARK.Attend.Entitys.Entity;
using PARK.Attend.Interfaces;
using PARK.Common.Core.Manager;
using PARK.Common.Enum;
using PARK.Dependency;
using PARK.DynamicApiController;
using PARK.FriendlyException;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace PARK.Attend
{
    /// <summary>
    /// 考勤规则
    /// </summary>
    [ApiDescriptionSettings(Tag = "Attend", Name = "AttendRule", Order = 200)]
    [Route("api/Attend/[controller]")]
    public class AttendRuleService : IAttendRuleService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<AttendRuleEntity> _attendRuleRepository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;

        public AttendRuleService(ISqlSugarRepository<AttendRuleEntity> attendRuleRepository, IUserManager userManager)
        {
            _attendRuleRepository = attendRuleRepository;
            _db = _attendRuleRepository.Context;
            _userManager = userManager;
        }

        #region Get
        /// <summary>
        /// 获取考勤规则
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpGet("")]
        public async Task<dynamic> GetInfo()
        {
            var userInfo = await _userManager.GetUserInfo();
            var entity = await _db.Queryable<AttendRuleEntity>().FirstAsync(p => p.CompanyId == userInfo.companyId);
            var output = entity.Adapt<AttendRuleInfoOutput>();
            return output;
        }

        #endregion

        #region Post
        /// <summary>
        /// 新建考勤规则
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPost("")]
        public async Task Create([FromBody] AttendRuleCrInput input)
        {
            var userInfo = await _userManager.GetUserInfo();
            int isOk=0;
            try
            {
                var entity = input.Adapt<AttendRuleEntity>();
                if (userInfo != null && userInfo.UserAdminFlag != 2 && !string.IsNullOrEmpty(userInfo.companyId))
                {
                    if (string.IsNullOrEmpty(input.Id))
                    {
                        entity.Create();
                        entity.CompanyId = userInfo.companyId;
                        entity.CompanyName = userInfo.companyName;
                        isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
                        if (!(isOk > 0))
                            throw PARKException.Oh(ErrorCode.COM1000);
                    }
                    else
                    {
                        entity.LastModify(input.Id);
                        isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
                        if (!(isOk > 0))
                            throw PARKException.Oh(ErrorCode.COM1001);
                    }

                }
            }
            catch (Exception ex)
            {

                throw;
            }

            #endregion
        }
    }
}
